package com.mhtvs;

import java.util.HashMap;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import com.mhtvs.MhtvsActivity;
import com.mhtvs.Database.PortForwards;
import com.mhtvs.Database.Servers;

//---------------------------------------------------------------
//Development source for MHTVS CLIENT
//"Android client for use with MHTVS"
//Copyright (c) 2011, Doug Thompson, perladdict@gmail.com
//All rights reserved.
//
//http://code.google.com/p/mhtvs/
//
//View Demo here:
//http://www.youtube.com/watch?v=v4M2uVrMrGk
//
//Redistribution and use in source and binary forms, with or
//without modification, are permitted provided that the following
//conditions are met:
//
//* Redistributions of source code must retain the above
//  copyright notice, this list of conditions and the
//  following disclaimer.
//* Redistributions in binary form must reproduce the
//  above copyright notice, this list of conditions and
//  the following disclaimer in the documentation and/or
//  other materials provided with the distribution.
//* Neither the name of the developer nor the names
//  of its contributors may be used to endorse or promote
//  products derived from this software without specific
//  prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
//CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
//MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
//SUCH DAMAGE.
//---------------------------------------------------------------


public class TunnelInfo extends MhtvsActivity {

	public static HashMap<String,String> sqliteConfsHash = new HashMap<String,String>();
	
	private static void populateHASHMAP(String key, String value) throws Exception{
		sqliteConfsHash.put(key, value);
	}

	public static HashMap<String, String> getAllDetailsInHASH() throws Exception {
		try { buildServerDetails(); } catch(Exception ex) { ex.printStackTrace(); }
		return sqliteConfsHash;
	}
		
	public static void buildServerDetails() throws Exception {	
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		queryBuilder.setTables( Servers.SERVER_TABLE_NAME + "," + PortForwards.PORTFORWARDS_TABLE_NAME );
		// WHERE CLAUSE:
		String inWhere = Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_ID + " = " + PortForwards.PORTFORWARDS_TABLE_NAME+ "." + PortForwards.PORTFORWARDS_ID_KEY;
		queryBuilder.appendWhere(inWhere);

		// Get the database and run the query
		SQLiteDatabase db = mDatabase.getReadableDatabase();
		String asColumnsToReturn[] = {  
				Servers.SERVER_TABLE_NAME + "." +Servers.SERVER_ADDRESS, 
				Servers.SERVER_TABLE_NAME + "." +Servers.SERVER_INTERNAL_ADDRESS,
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_ID, 
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_ACTIVE_STATE,
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_USER,
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_PASSWORD,
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_PORT,  		
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_SALT,
				Servers.SERVER_TABLE_NAME + "." + Servers.SERVER_PAGE,
				Servers.SERVER_TABLE_NAME + "." +Servers.SERVER_ACTIVE_INTERNAL_STATE,
				PortForwards.PORTFORWARDS_TABLE_NAME+ "." + PortForwards.PORTFORWARDS_ID_KEY,
				PortForwards.PORTFORWARDS_TABLE_NAME+ "." + PortForwards.PORTFORWARDS_LOCAL,
				PortForwards.PORTFORWARDS_TABLE_NAME+ "." + PortForwards.PORTFORWARDS_REMOTE
		};
		final Cursor c = queryBuilder.query(db, asColumnsToReturn, null, null, null, null, Servers.DEFAULT_SORT_ORDER);
		// Display the results by adding some TableRows to the existing table layout
		if(c.moveToFirst()) {
			for(int i = 0; i< c.getCount(); i++) {
				int activeServer = c.getInt(c.getColumnIndex(Servers.SERVER_ACTIVE_STATE)); 
				if (activeServer == 1 ) {
					int serverid = c.getInt(c.getColumnIndex(Servers.SERVER_ID));
					int portforwardsLOCAL = c.getInt(c.getColumnIndex(PortForwards.PORTFORWARDS_LOCAL));
					int portforwardsREMOTE = c.getInt(c.getColumnIndex(PortForwards.PORTFORWARDS_REMOTE));
					int  serverPort = c.getInt(c.getColumnIndex(Servers.SERVER_PORT));			
					String serverUserName = c.getString(c.getColumnIndex(Servers.SERVER_USER));
					String serverPage = c.getString(c.getColumnIndex(Servers.SERVER_PAGE));
					String masterPassword = c.getString(c.getColumnIndex(Servers.SERVER_PASSWORD));
					//String saltPassword = c.getString(c.getColumnIndex(Servers.SERVER_SALT));
					String serverAddress = c.getString(c.getColumnIndex(Servers.SERVER_ADDRESS));
					int activeInternalState = c.getInt(c.getColumnIndex(Servers.SERVER_ACTIVE_INTERNAL_STATE));
					String serverInternalAddress = null;
					if (activeInternalState == 1) {
						serverInternalAddress = c.getString(c.getColumnIndex(Servers.SERVER_INTERNAL_ADDRESS));
					} else {
						serverInternalAddress = "127.0.0.1";
					}
					populateHASHMAP("serverUserName"        + String.valueOf(i), serverUserName);
					populateHASHMAP("serverPage"            + String.valueOf(i), String.valueOf(serverPage));
					populateHASHMAP("serverAddress"         + String.valueOf(i), serverAddress);
					populateHASHMAP("serverPort"            + String.valueOf(i), String.valueOf(serverPort));
					populateHASHMAP("masterPassword"        + String.valueOf(i), masterPassword);
					populateHASHMAP("serverInternalAddress" + String.valueOf(i), serverInternalAddress);
					populateHASHMAP("portforwardsLOCAL"     + String.valueOf(i), String.valueOf(portforwardsLOCAL));
					populateHASHMAP("portforwardsREMOTE"    + String.valueOf(i), String.valueOf(portforwardsREMOTE));
				}
			c.moveToNext();	
			}
		}
		c.close();
		db.close();
	} 
}
